from django.test import TestCase

from mdcssport.mdcsapp import elo_utils 

"""
Testira stranu za dodavanje poena igracima
"""
class EloUtilsTestCase(TestCase):
    
    def setUp(self):
        pass

    def runTest(self):
        self.test_calculate_elo()
        self.test_calculate_elo_bonus()

    def test_calculate_elo(self):
        offset1, offset2 = elo_utils.calculate_elo(1200, 1200, 1)
        self.assertAlmostEqual(16, offset1, 0)
        self.assertAlmostEqual(-16, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1200, 1200, 3)
        self.assertAlmostEqual(-16, offset1, 0)
        self.assertAlmostEqual(16, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1400, 1000, 1)
        self.assertAlmostEqual(3, offset1, 0)
        self.assertAlmostEqual(-3, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1400, 1000, 3)
        self.assertAlmostEqual(-29, offset1, 0)
        self.assertAlmostEqual(29, offset2, 0)

        # testira nereseno
        offset1, offset2 = elo_utils.calculate_elo(1200, 1200, 2)
        self.assertAlmostEqual(0, offset1, 0)
        self.assertAlmostEqual(0, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1400, 1000, 2)
        self.assertAlmostEqual(-13, offset1, 0)
        self.assertAlmostEqual(13, offset2, 0)

        # ako winner nije ni 1 ni 2, oba igraca gube poene
        offset1, offset2 = elo_utils.calculate_elo(1200, 1200, 0)
        self.assertAlmostEqual(-16, offset1, 0)
        self.assertAlmostEqual(-16, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1400, 1000, 0)
        self.assertAlmostEqual(-29, offset1, 0)
        self.assertAlmostEqual(-3, offset2, 0)

    def test_calculate_elo_bonus(self):
        offset1, offset2 = elo_utils.calculate_elo(1200, 1200, 1, True)
        self.assertAlmostEqual(24, offset1, 0)
        self.assertAlmostEqual(-8, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1200, 1200, 3, True)
        self.assertAlmostEqual(-8, offset1, 0)
        self.assertAlmostEqual(24, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1400, 1000, 1, True)
        self.assertAlmostEqual(4, offset1, 0)
        self.assertAlmostEqual(-1, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1400, 1000, 3, True)
        self.assertAlmostEqual(-15, offset1, 0)
        self.assertAlmostEqual(44, offset2, 0)

        # testira nereseno
        offset1, offset2 = elo_utils.calculate_elo(1200, 1200, 2, True)
        self.assertAlmostEqual(0, offset1, 0)
        self.assertAlmostEqual(0, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1400, 1000, 2, True)
        self.assertAlmostEqual(-6.5, offset1, 0)
        self.assertAlmostEqual(19.5, offset2, 0)

        # ako winner nije ni 1 ni 2, oba igraca gube poene
        offset1, offset2 = elo_utils.calculate_elo(1200, 1200, 0, True)
        self.assertAlmostEqual(-8, offset1, 0)
        self.assertAlmostEqual(-8, offset2, 0)

        offset1, offset2 = elo_utils.calculate_elo(1400, 1000, 0, True)
        self.assertAlmostEqual(-15, offset1, 0)
        self.assertAlmostEqual(-1, offset2, 0)